package com.wgu.crawl3.service.impl;

import com.wgu.crawl.util.CrawlUtil;
import com.wgu.crawl3.service.CrawlService3;
import com.wgu.modules.dto.AddBookDTO;
import com.wgu.modules.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Element;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: w
 * @Date: 2019/7/4 9:20
 * @Version 1.0
 */
@Service
@Slf4j
public class CrawlServiceImpl3 implements CrawlService3 {
    @Resource
    private BookService bookService;

    @Override
    @Async
    public void book2(String url) {
        //爬取当前页面
        String result = CrawlUtil.crawl(url,"UTF-8");
        
    }

    @Override
    public void book() {
        String bookUrl = "https://www.qidian.com/free";
        //爬取当前页面
        String result = CrawlUtil.crawl(bookUrl,"UTF-8");

        List<Element> contentList = CrawlUtil.analyByRole(bookUrl,result,"#limit-list .book-mid-info h4 a");

        contentList.forEach(x->{
            String bookName = x.text();
            log.info(bookName);
            AddBookDTO dto = new AddBookDTO();
            dto.setBookName(bookName);
            bookService.addBook(dto);
        });
    }
}
